<?php
 class CompleteHelper extends AppHelper {
  var $helpers = array('Ajax','Form');
  function cityAutocomplete($num_results = 7,$do_redirect = false)
  {
  		echo("\n" . '<script type="text/javascript">' . "\n");
  		echo('//<![CDATA['. "\n");
		echo(' function cityAutocompleteUpdateElement(field,i) {'. "\n");
		echo('   str = i.id;'. "\n");
		echo("   document.getElementById('CityId').value = str.substring(str.indexOf('_') + 1);". "\n");
		if($do_redirect)
		{
		 echo("   window.location='/cities/view/' + str.substring(str.indexOf('_') + 1);");
		}
		echo(' }' . "\n");
		echo('//]]>' . "\n");
		echo('</script>' . "\n");
		  		
  	    echo $this->Ajax->autoComplete('City.ascii_name', '/cities/autoComplete/' . $num_results, array('class' => 'auto_complete', 'afterUpdateElement' => 'cityAutocompleteUpdateElement', 'frequency' => 0.1));
   		echo $this->Form->hidden('City.id');
  }
  
  function siteAutocomplete($user_results =4 , $city_results = 8,$do_redirect = true)
  {
  		
  		echo("\n" . '<script type="text/javascript">' . "\n");
  		echo('//<![CDATA['. "\n");
  		echo(' function focused(obj) {');
  		echo('if (obj.defaultValue==obj.value) { ');
  		echo('obj.value = ""; } ');
  		echo(' }');
		echo(' function siteAutocompleteUpdateElement(field,i) {'. "\n");
		echo('   str = i.id;'. "\n");
		echo('   field.value = \'\'; ');
		echo('   if(str.indexOf(\'profileid_\') == 0) {');
  		if($do_redirect)
		{
		 echo("   window.location='/profiles/view/' + str.substring(str.indexOf('_') + 1);");
		}
		echo('   }' . "\n");
		echo('   else if (str.indexOf(\'cityid_\') == 0) {');
 		if($do_redirect)
		{
		 echo("   window.location='/cities/view/' + str.substring(str.indexOf('_') + 1);");
		}
		echo('   }' . "\n");
		
		//echo("   document.getElementById('CityId').value = str.substring(str.indexOf('_') + 1);". "\n");
		
		echo(' }' . "\n");
		echo('//]]>' . "\n");
		echo('</script>' . "\n");
		
  		
  	    echo $this->Ajax->autoComplete('query', '/cities/siteAutoComplete/' . $user_results . '/' . $city_results, array('name' => 'data[query]', 'value' => 'Search: Cities and Users', 'onfocus' => 'focused(this);', 'class' => 'auto_complete', 'afterUpdateElement' => 'siteAutocompleteUpdateElement', 'frequency' => 0.1));
   		
  }
  
  
}
?>